/*
Copyright [2020] [https://www.xiaonuo.vip]

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

  http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.

Snowy采用APACHE LICENSE 2.0开源协议，您在使用过程中，需要注意以下几点：

1.请不要删除和修改根目录下的LICENSE文件。
2.请不要删除和修改Snowy源码头部的版权声明。
3.请保留源码和相关描述文件的项目出处，作者声明等。
4.分发源码时候，请注明软件出处 https://gitee.com/xiaonuobase/snowy
5.在修改包名，模块名称，项目代码等时，请注明软件出处 https://gitee.com/xiaonuobase/snowy
6.若您的项目无法满足以上几点，可申请商业授权，获取Snowy商业授权许可，请在官网购买授权，地址为 https://www.xiaonuo.vip
 */
package vip.xiaonuo.modular.system.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import vip.xiaonuo.core.annotion.BusinessLog;
import vip.xiaonuo.core.annotion.Permission;
import vip.xiaonuo.core.enums.LogAnnotionOpTypeEnum;
import vip.xiaonuo.core.pojo.page.PageResult;
import vip.xiaonuo.core.pojo.response.ResponseData;
import vip.xiaonuo.core.pojo.response.SuccessResponseData;
import vip.xiaonuo.modular.system.entity.BizMat;
import vip.xiaonuo.modular.system.entity.BizOrder;
import vip.xiaonuo.modular.system.param.BizStockParam;
import vip.xiaonuo.modular.system.service.BizMatService;
import vip.xiaonuo.modular.system.service.BizOrderService;
import vip.xiaonuo.modular.system.service.BizStockService;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import vip.xiaonuo.modular.system.vo.BizMatDown;
import vip.xiaonuo.modular.system.vo.BizOrderSendVO;
import vip.xiaonuo.modular.system.vo.BizStockVO;
import vip.xiaonuo.modular.system.vo.SysUserDown;
import vip.xiaonuo.sys.modular.user.entity.SysUser;
import vip.xiaonuo.sys.modular.user.entity.SysUserRole;
import vip.xiaonuo.sys.modular.user.service.SysUserRoleService;
import vip.xiaonuo.sys.modular.user.service.SysUserService;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 库存表控制器
 *
 * @author can
 * @date 2022-03-19 14:12:32
 */
@RestController
public class BizStockController {

    @Resource
    private BizStockService bizStockService;

    @Resource
    private BizOrderService bizOrderService;

    @Resource
    private BizMatService bizMatService;

    @Resource
    private SysUserService sysUserService;

    @Resource
    private SysUserRoleService sysUserRoleService;
    /**
     * 查询库存表
     *
     * @author can
     * @date 2022-03-19 14:12:32
     */
    @GetMapping("/bizStock/page")
    @BusinessLog(title = "库存表_查询", opType = LogAnnotionOpTypeEnum.QUERY)
    public ResponseData page(BizStockParam bizStockParam) {
        PageResult<BizStockVO> page = bizStockService.page(bizStockParam);
        return new SuccessResponseData(page);
    }

    /**
     * 添加库存表
     *
     * @author can
     * @date 2022-03-19 14:12:32
     */
    @PostMapping("/bizStock/add")
    @BusinessLog(title = "库存表_增加", opType = LogAnnotionOpTypeEnum.ADD)
    public ResponseData add(@RequestBody @Validated(BizStockParam.add.class) BizStockParam bizStockParam) {
            bizStockService.add(bizStockParam);
        return new SuccessResponseData();
    }

    /**
     * 删除库存表，可批量删除
     *
     * @author can
     * @date 2022-03-19 14:12:32
     */
    @PostMapping("/bizStock/delete")
    @BusinessLog(title = "库存表_删除", opType = LogAnnotionOpTypeEnum.DELETE)
    public ResponseData delete(@RequestBody @Validated(BizStockParam.delete.class) List<BizStockParam> bizStockParamList) {
            bizStockService.delete(bizStockParamList);
        return new SuccessResponseData();
    }

    /**
     * 编辑库存表
     *
     * @author can
     * @date 2022-03-19 14:12:32
     */
    @PostMapping("/bizStock/edit")
    @BusinessLog(title = "库存表_编辑", opType = LogAnnotionOpTypeEnum.EDIT)
    public ResponseData edit(@RequestBody @Validated(BizStockParam.edit.class) BizStockParam bizStockParam) {
            bizStockService.edit(bizStockParam);
        return new SuccessResponseData();
    }

    /**
     * 查看库存表
     *
     * @author can
     * @date 2022-03-19 14:12:32
     */
    @GetMapping("/bizStock/detail")
    @BusinessLog(title = "库存表_查看", opType = LogAnnotionOpTypeEnum.DETAIL)
    public ResponseData detail(@Validated(BizStockParam.detail.class) BizStockParam bizStockParam) {
        return new SuccessResponseData(bizStockService.detail(bizStockParam));
    }

    /**
     * 库存表列表
     *
     * @author can
     * @date 2022-03-19 14:12:32
     */
    @GetMapping("/bizStock/list")
    @BusinessLog(title = "库存表_列表", opType = LogAnnotionOpTypeEnum.QUERY)
    public ResponseData list(BizStockParam bizStockParam) {
        return new SuccessResponseData(bizStockService.list(bizStockParam));
    }


    /**
     * 库存表列表
     *
     * @author can
     * @date 2022-03-19 14:12:32
     */
    @GetMapping("/bizStock/get-mat-list")
    @BusinessLog(title = "库存表_列表", opType = LogAnnotionOpTypeEnum.QUERY)
    public ResponseData getMatList(BizStockParam bizStockParam) {
        List<BizMat> list = bizMatService.list(new QueryWrapper<BizMat>().lambda().select(BizMat::getId, BizMat::getMatName));
        ArrayList<BizMatDown> bizMatDowns = new ArrayList<>();
        list.forEach(e -> {
            BizMatDown one = new BizMatDown();
            one.setId(e.getId());
            one.setMatCode(e.getMatCode());
            one.setMatName(e.getMatName());
            bizMatDowns.add(one);
        });
        return new SuccessResponseData(bizMatDowns);
    }
    /**
     * 导出系统用户
     *
     * @author can
     *
     *
     * * @date 2022-03-19 14:12:32
     */
    @GetMapping("/bizStock/export")
    @BusinessLog(title = "库存表_导出", opType = LogAnnotionOpTypeEnum.EXPORT)
    public void export(BizStockParam bizStockParam) {
        bizStockService.export(bizStockParam);
    }

    /**
     * 派送接口
     * @param sendVo
     * @return
     */
    @PostMapping("/bizStock/send")
    public SuccessResponseData toSend(@RequestBody BizOrderSendVO sendVo) {
        List<Long> ids = sendVo.getIds();
        Long sendUserId = sendVo.getSendUserId();
        UpdateWrapper<BizOrder> updateWrapper = new UpdateWrapper<>();
        updateWrapper.lambda().in(BizOrder::getId,ids).set(BizOrder::getSendUserId,sendUserId).set(BizOrder::getStatus,2);
        bizOrderService.update(updateWrapper);
        return new SuccessResponseData();
    }

    @PostMapping("/bizStock/complete")
    public SuccessResponseData toCompleted(@RequestBody BizOrder bizOrder) {
        UpdateWrapper<BizOrder> updateWrapper = new UpdateWrapper<>();
        updateWrapper.lambda().eq(BizOrder::getId,bizOrder.getId()).set(BizOrder::getStatus,3);
        bizOrderService.update(updateWrapper);
        return new SuccessResponseData();
    }

    @GetMapping("/bizStock/send-user-down")
    public SuccessResponseData sendUserDown() {
        // 工作人员和志愿者可以派送
        List<Long> ids = sysUserRoleService.list(new QueryWrapper<SysUserRole>().lambda().eq(SysUserRole::getRoleId, 1502858374831149058L))
                .stream().map(SysUserRole::getUserId).collect(Collectors.toList());

        List<Long> ids2 = sysUserService.list(new QueryWrapper<SysUser>().lambda().eq(SysUser::getIsVolunteer, 1))
                .stream().map(SysUser::getId).collect(Collectors.toList());
        HashSet<Long> totalIds = new HashSet<>();
        totalIds.addAll(ids);
        totalIds.addAll(ids2);

        QueryWrapper<SysUser> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().in(SysUser::getId,totalIds);
        List<SysUser> list = sysUserService.list(queryWrapper);
        List<SysUserDown> sysUsers = new ArrayList<>();
        list.forEach(e -> {
            SysUserDown one = new SysUserDown();
            one.setId(e.getId());
            one.setUserName(e.getName());
            sysUsers.add(one);
        });
        return new SuccessResponseData(sysUsers);
    }

}
